<?php
/**
 * This file is part of League of Legends PHP Inibin Parser.
 *
 * League of Legends PHP Inibin Parser is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * League of Legends PHP Inibin Parser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with League of Legends PHP Inibin Parser.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * League of Legends Client-Database parser
 * Custom Functions
 *
 * @author Marcel Klimek <klimek@secondred.de>
 * @copyright Marcel Klimek
 * @version $Id: class.general.php 3 2011-11-12 10:07:44Z naffto88@gmail.com $
 */

class general
{

    /**
     * This method searces recursive for all files in the given directory
     * with the specific $strSearch within the name
     *
     * @param string $strVerzeichnis
     * @param string $strSearch
     * @param array $arrBlacklist
     * @return string
     */
    public function recursiveDir($strVerzeichnis,$strSearch,$arrWhitelist)
    {
        $arrFiles = array();
        $handle =  opendir($strVerzeichnis);

        while ($datei = readdir($handle))
        {
            if ($datei != "." && $datei != "..")
            {
                if (is_dir($strVerzeichnis. DIRECTORY_SEPARATOR .$datei))
                {
                    $arrFiles = array_merge($arrFiles,$this->recursiveDir($strVerzeichnis. DIRECTORY_SEPARATOR .$datei,$strSearch,$arrWhitelist));
                } else
                {
                    if (strpos($datei, $strSearch) !== FALSE && array_key_exists(str_replace($strSearch,'',$datei),$arrWhitelist))
                    {
                        $arrFiles[] = $strVerzeichnis. DIRECTORY_SEPARATOR .$datei;
                    }
                }
            }
        }
        closedir($handle);
        $arrFiles = $this->checkForNewestVersion($arrFiles,$strSearch,$arrWhitelist);
        return $arrFiles;
    }


    /**
     * This Method will clean the full Champion inibin array from older files
     *
     * @param array $arrFiles of all Champio-Files
     * @return array of Newest unique Champions
     */
    public function checkForNewestVersion($arrFiles,$strSearch,$arrWhitelist)
    {
        $arrReturn = array();
        
        foreach ($arrFiles AS $iKey => $strFilePath)
        {
            $arrFilepath = explode("\\",$strFilePath);
            $strFilename = str_replace($strSearch, "", $arrFilepath[count($arrFilepath) - 1]);
            //now we have the name, just put it in the return array
            $strRealName = $arrWhitelist[$strFilename];
            
            $arrReturn[$strRealName] = $strFilePath;
        }
        return $arrReturn;
    }
}

?>
